package com.ycy.lucene;

import com.ycy.lucene.search.BasicQuery;
import com.ycy.lucene.search.PageSizeQuery;
import com.ycy.lucene.search.RangeQuery;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.*;
import org.junit.Before;
import org.junit.Test;

public class IndexCreateDemo {
  com.ycy.lucene.index.IndexCreateDemo indexCreateDemo;
  BasicQuery basicQuery;
  RangeQuery rangeQuery;
  PageSizeQuery pageQuery;

  @Before
  public void before() {
    indexCreateDemo = new com.ycy.lucene.index.IndexCreateDemo();
    basicQuery = new BasicQuery();
    rangeQuery = new RangeQuery();
    pageQuery = new PageSizeQuery();
  }

  @Test
  public void create() {
    indexCreateDemo.createIndex();
  }

  @Test
  public void search() {
    basicQuery.search0(); //精确查询
    basicQuery.search1(); //短语查询
    basicQuery.wildcardQuery(); //通配符
    basicQuery.search3(); //是非
  }

  @Test
  public void search1() {
    System.out.println("范围查询");
    rangeQuery.search01(); //范围查询
    rangeQuery.search02(); //范围查询  数值类型
    rangeQuery.search03(); //数值 精确查找

  }

  @Test
  public void searchPage() throws ParseException {
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    builder.add(new TermQuery(new Term("name", "店")), BooleanClause.Occur.MUST);
    builder.add(new TermQuery(new Term("name", "人")), BooleanClause.Occur.MUST_NOT);
    BooleanQuery query = builder.build();

//    String queryStr = "name:\"MC\"";
//    Analyzer analyzer = new StandardAnalyzer();
//    QueryParser queryParser = new QueryParser(null, analyzer);
//    Query query = queryParser.parse(queryStr);

    //按照id 排序
    pageQuery.searchPage(query, new Sort(new SortField(null, SortField.Type.DOC)), 1, 20);

    //按照id 排序
    pageQuery.searchPage(query, new Sort(new SortField(null, SortField.Type.DOC)), 1, 5);

    //按照id 排序
    pageQuery.searchPage(query, new Sort(new SortField(null, SortField.Type.DOC)), 2, 5);

    //按照id 排序
    pageQuery.searchPage(query, new Sort(new SortField(null, SortField.Type.DOC)), 3, 5);

  }

  @Test
  public void delete() {
    System.out.println("delete");
  }


}
